import socket
from scapy.all import *
from getmac import get_mac_address
from getip import get_ip


def HostAddress():
    eth_mac = get_mac_address(interface="eth0")
    hostname = socket.gethostname()
    ip = get_ip("eth0")
    return (eth_mac, ip)

# ARP scapy

def ArpScan():
    mac = HostAddress()[0]
    ip = HostAddress()[1]
    iface = "eth0"
    
    
    print(mac, ip)

    ipSplit = ip.split('.') 
    ips = []
    for i in range(1, 255):
        ip = ipSplit[0] + '.' + ipSplit[1] + '.' + ipSplit[2] + '.' + str(i)
        ips.append(ip)

    resp = srp(Ether(src=mac, dst="FF:FF:FF:FF:FF:FF")/ARP(op=1,hwsrc=mac,hwdst='00:00:00:00:00:00',pdst=ips),iface=iface,timeout=2,verbose=False)
    respAns = resp[0].res

    lives = []
    # recv packet amount
    nums = len(respAns)
    print("++++++++++++")
    print("ARP Result: ")
    print("===============")

    for x in range(nums):
        IP = respAns[x][1][1].fields['psrc']
        MAC = respAns[x][1][1].fields['hwsrc']
        lives.append([IP,MAC])
        print("Alive:", x+1)

        print("IP: " + IP + '\n\n' + "MAC: " + MAC)
        print("===============")

    print("++++++++++++")

if __name__ == '__main__':
    ArpScan()
